package com.wdf.rule.drools;

/**
 * @author wangdingfu
 * @date 2023-11-13 22:03:42
 */
public class DynamicTest {


    private static final String sourceCode = "package com.wdf.rule.order;\n" +
            "\n" +
            "\n" +
            "/**\n" +
            " * @author wangdingfu\n" +
            " * @date 2023-11-09 17:44:44\n" +
            " */\n" +
            "public class OrderTest {\n" +
            "\n" +
            "\n" +
            "    /**\n" +
            "     * 订单金额\n" +
            "     */\n" +
            "    private int amount;\n" +
            "\n" +
            "    /**\n" +
            "     * 积分\n" +
            "     */\n" +
            "    private int score;\n" +
            "\n" +
            "    public int getAmount() {\n" +
            "        return amount;\n" +
            "    }\n" +
            "\n" +
            "    public void setAmount(int amount) {\n" +
            "        this.amount = amount;\n" +
            "    }\n" +
            "\n" +
            "    public int getScore() {\n" +
            "        return score;\n" +
            "    }\n" +
            "\n" +
            "    public void setScore(int score) {\n" +
            "        this.score = score;\n" +
            "    }\n" +
            "}\n";

    public static Class<?> orderTestClass() {
        return commonClass("com.wdf.rule.order.OrderTest", sourceCode);
    }

    public static Class<?> commonClass(String pkg, String code) {
        CustomClassCompiler compiler = CustomClassCompiler.newInstance(null);

        try {
            compiler.addSource(pkg, code);

            compiler.compile(pkg);

            return compiler.getClassByName(pkg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
